package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.MobileStaffPermission;
import com.xmy.cultivate.entity.StaffPermission;
import com.xmy.cultivate.mapper.MobileRolePermissionMapper;
import com.xmy.cultivate.mapper.MobileStaffPermissionMapper;
import com.xmy.cultivate.mapper.RolePermissionMapper;
import com.xmy.cultivate.mapper.StaffPermissionMapper;
import com.xmy.cultivate.service.IMobileStaffPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工权限 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-26
 */
@Service
public class MobileStaffPermissionServiceImpl extends ServiceImpl<MobileStaffPermissionMapper, MobileStaffPermission> implements IMobileStaffPermissionService {
    @Autowired
    MobileStaffPermissionMapper mobileStaffPermissionMapper;

    @Autowired
    MobileRolePermissionMapper mobileRolePermissionMapper;

    @Override
    public List<Long> getStaffPermissionAllByStaffId(String staffId) {
        return mobileStaffPermissionMapper.getStaffPermissionAllByStaffId(staffId);
    }

    @Override
    public List<Long> getStaffPermissionAll(String staffId, String roleId) {
        return mobileStaffPermissionMapper.getStaffPermissionAll(staffId, roleId);
    }

    @Override
    public List<Long> getStaffPermissionByStaffId(String staffId, Integer type) {
        return mobileStaffPermissionMapper.getStaffPermissionByStaffId(staffId, type);
    }

    /**
     * 个人权限新增/修改
     *
     * @param staffId
     * @param staffPermissionList
     * @return
     */
    @Override
    public Boolean updateStaffPermissionByStaffId(String staffId, List<Long> staffPermissionList) {
        List<Long> rolePermissionList = mobileRolePermissionMapper.getRolePermissionAllByStaffId(staffId);

        List<Long> staffPermissionAllList = mobileStaffPermissionMapper.getStaffPermissionAllByStaffId(staffId);

        if (staffPermissionList != null && staffPermissionAllList != null) {
            boolean isequal = CollectionUtils.isEqualCollection(staffPermissionList, staffPermissionAllList);
            if (isequal) {
                System.err.println("一致不继续");
                return false;
            }
        }

        // 情况一：新增个人权限
        List<Long> newPermissions = new ArrayList<>(staffPermissionList);
        newPermissions.removeAll(rolePermissionList); // 过滤掉角色已有的权限
        List<Long> staffPermissionSubjoinList1 = mobileStaffPermissionMapper.getStaffPermissionByStaffId(staffId);
        for (Long newPermission : newPermissions) {
            if (!staffPermissionSubjoinList1.contains(newPermission)) {
                MobileStaffPermission staffPermission = new MobileStaffPermission();
                staffPermission.setStaffId(Long.parseLong(staffId));
                staffPermission.setPermissionId(newPermission);
                staffPermission.setType(1); // 标记为新增权限
                mobileStaffPermissionMapper.insert(staffPermission);
            }
        }

        // 情况二：减少个人权限
        List<Long> missingPermissions = new ArrayList<>(rolePermissionList);
        missingPermissions.removeAll(staffPermissionList); // 找出被去掉的权限
        List<Long> staffPermissionSubjoinList2 = mobileStaffPermissionMapper.getStaffPermissionByStaffId(staffId, 2);
        for (Long missingPermission : missingPermissions) {
            if (!staffPermissionSubjoinList2.contains(missingPermission)) {
                MobileStaffPermission staffPermission = new MobileStaffPermission();
                staffPermission.setStaffId(Long.parseLong(staffId));
                staffPermission.setPermissionId(missingPermission);
                staffPermission.setType(2); // 标记为减少权限
                mobileStaffPermissionMapper.insert(staffPermission);
            }
        }

        // 情况三：恢复被减少的权限（删除的恢复）
        List<Long> permissionsToRestore = new ArrayList<>(staffPermissionList);
        permissionsToRestore.retainAll(staffPermissionSubjoinList2); // 找出需要恢复的权限
        for (Long permissionToRestore : permissionsToRestore) {
            mobileStaffPermissionMapper.delete(new QueryWrapper<MobileStaffPermission>()
                    .eq("staff_id", staffId)
                    .eq("permission_id", permissionToRestore)
                    .eq("type", 2)); // 删除type为2的记录，即恢复权限
        }

        // 情况四：恢复被新增的权限（新增的删除）
        List<Long> permissionsToRemove = new ArrayList<>(staffPermissionSubjoinList1);
        permissionsToRemove.removeAll(staffPermissionList); // 找出需要删除的新增权限
        for (Long permissionToRemove : permissionsToRemove) {
            mobileStaffPermissionMapper.delete(new QueryWrapper<MobileStaffPermission>()
                    .eq("staff_id", staffId)
                    .eq("permission_id", permissionToRemove)
                    .eq("type", 1)); // 删除type为1的记录，即删除新增权限
        }

        return true;
    }
}
